local nafeng = fk.CreateSkill{
  name = "hanqing1__nafeng",
  tags = { Skill.Quest },
}

Fk:loadTranslationTable{
  ["hanqing1__nafeng"] = "纳风",
  [":hanqing1__nafeng"] = "使命技，中央区移去牌时，若移去的牌数不小于你上次发动，你可以使用中央区的一张牌。<br>\
  ⬤　成功：发动五次〖纳风〗后，你获得一个将“获得”改为“使用”的〖纳风〗；<br>\
  ⬤　失败：五回合未发动〖纳风〗，你删除〖纳风〗的“使命技”标签，然后将“不小于”改为“不大于”。",
}

local DIY = require "packages/diy_utility/diy_utility"

nafeng:addEffect(DIY.BeforeCenterAreaRemove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(nafeng.name) and #player.room:getBanner("@$CenterArea") > 0 and
      #data.ids >= player:getMark("@hanqing__nafeng")
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseRealCard(player, {
      pattern = room:getBanner("@$CenterArea"),
      skill_name = nafeng.name,
      prompt = "#hanqing__nafeng-use",
      extra_data = {
        bypass_times = true,
        expand_pile = room:getBanner("@$CenterArea"),
        extraUse = true,
      },
      cancelable = true,
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "hanqing__nafeng_fail_count", 0)
    room:setPlayerMark(player, "@hanqing__nafeng", #data.ids)
    room:useCard(event:getCostData(self).extra_data)
    if player:usedSkillTimes(nafeng.name, Player.HistoryGame) > 4 and not player.dead and player:hasSkill(nafeng.name, true) then
      room:notifySkillInvoked(player, nafeng.name, "special")
      player:setSkillUseHistory(nafeng.name, 0, Player.HistoryGame)
    end
  end,
})

nafeng:addEffect(fk.TurnEnd, {
  late_refresh = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(nafeng.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
      if player:usedSkillTimes(nafeng.name, Player.HistoryTurn) > 0 then
        room:setPlayerMark(player, "hanqing__nafeng_fail_count", 0)
      else
        room:addPlayerMark(player, "hanqing__nafeng_fail_count", 1)
      end
  end,
})

nafeng:addEffect(fk.EventTurnChanging, {
  late_refresh = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(nafeng.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player:getMark("hanqing__nafeng_fail_count") > 4 then
      room:notifySkillInvoked(player, nafeng.name, "negative")
      room:updateQuestSkillState(player, nafeng.name, true)
      room:handleAddLoseSkills(player, "-hanqing1__nafeng|hanqing3__nafeng", nil, true, false)
    end
  end,
})

return nafeng
